
package testemodulorede;

import java.io.*;
import java.net.*;
import java.util.*;

public class ServerModule implements LanModule{

    private int clientLimit;
    private ArrayList clientOutputStreams;
    private ArrayList players;
    private GamePanel game;
    private String serverPlayerConfig;

    public ServerModule( GamePanel _game, String _serverPlayerConfig, int _clientLimit ){

        game = _game;
        serverPlayerConfig = _serverPlayerConfig;
        clientLimit = _clientLimit;
        
    }

    public synchronized void tellEveryone(String message){

        Iterator it = clientOutputStreams.iterator();

        while(it.hasNext()){

            try{

                PrintWriter writer = (PrintWriter) it.next();
                writer.println(message);
                writer.flush();

            }
            catch(Exception ex){

                ex.printStackTrace();

            }

        }
        
    }

    public void startService(){

        clientOutputStreams = new ArrayList();
        players = new ArrayList();
        
        players.add( serverPlayerConfig );

        try{

            ServerSocket serverSock = new ServerSocket(12345);
            int temp = this.clientLimit;
            
            while( true ){// tmp-- > 0

                if( temp-- == 0){
                    break;
                }

                Socket clientSocket = serverSock.accept();

                PrintWriter writer = new PrintWriter(clientSocket.getOutputStream() );
                clientOutputStreams.add(writer);
                Thread t = new Thread( new ClientHandler( clientSocket, this ) );
                t.start();
                System.out.println("got a connection!");
                                
            }

            //ja tendo recebido as informações de todos os players
            //envia as informações do jogo a todos
            tellEveryone(createPlayersDataPkg());
            sendIds();
           
        }
        catch(Exception ex){

            ex.printStackTrace();

        }
        
    }

    public void incommingGameData( String _message ){

        if( _message.charAt( 0 ) == '1' ){

            addClientData( _message );
            System.out.println(" Atencao: " + _message + " foi adicionado a lista de clientes. \n" );

        }
        else{
            game.gameAction( _message );
            tellEveryone( _message );
        }


    }

    public void addClientData( String _message ){
        //add cliente a lista
        players.add(_message);
    }

    public String createPlayersDataPkg(){

        Iterator it = players.iterator();
        String pkg = "2:GAME_DATA";
        int id = 0;

        while(it.hasNext()){

            String tmp = (String)it.next();

            pkg += ":" + id++ + tmp.substring(13);

        }

        return pkg;

    }

    public void sendPackage(String _message){
        
        if( _message.charAt(0) == '8'){
            game.gameAction(_message);
        }

        tellEveryone(_message);
        
    }

    public synchronized void sendEspecificPackage(int _id, String _message){

        try{

            PrintWriter writer = (PrintWriter)clientOutputStreams.get( _id );
            writer.println(_message);
            writer.flush();

        }
        catch(Exception ex){

            ex.printStackTrace();

        }

    }

    public void sendIds(){

        int id = 0;

        while( id < clientLimit ){
            
            sendEspecificPackage( id, "9:" + (char)++id + ":ID" );

        }

    }

}
